/**
 * Copyright (c) 2012 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.android.wordraster.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BufferedReaderIterator implements Iterator<String>{
	private static Logger LOGGER = Logger.getLogger(BufferedReaderIterator.class.getName());
	
  private BufferedReader myInputStream;
  private String myLine = null;

  public BufferedReaderIterator(BufferedReader aInputStream) {
    super();
    myInputStream = aInputStream;
  }

  @Override
  public boolean hasNext() {
    try {
      do{
        myLine = myInputStream.readLine();
        if(myLine == null) return false;
      }while(myLine.length() < 3);

      if(myLine.length() > 8){
        myLine = null;
      }
    } catch (IOException e) {
      LOGGER.log( Level.SEVERE, "Could not read line from dictionary", e);
      myLine = null;
    } finally {
      if(myLine == null){
        try {
          myInputStream.close();
        } catch (IOException e) {
          LOGGER.log( Level.WARNING, "Could not close reader", e);
        }
      }
    }
    return myLine != null;
  }

  @Override
  public String next() {
    return myLine;
  }

  @Override
  public void remove() {
  }
}
